(function( _ , $ ){
	
	/**
	 * 背景设置器
	 */
	$.fn.background = function(_setting){
		var current = $(this);
		var setting = {
			type : "repeat",	//背景布局方式	repeat|repeat-x|repeat-y|center|center-repeat|stretch
			img : null
		};
		$.extend(setting,_setting);	//合并属性
		
		current.css("background","none");	//清楚默认属性
		current.children(".jdmer_background_img").remove();
		if(setting.img){
			if(setting.type == "repeat"){
				current.css("background","url('"+setting.img+"')");
			}else if(setting.type == "repeat-x"){
				current.css("background-image","url('"+setting.img+"')");
				current.css("background-repeat",setting.type);
			}else if(setting.type == "repeat-y"){
				current.css("background-image","url('"+setting.img+"')");
				current.css("background-repeat",setting.type);
			}else if(setting.type == "center"){
				current.css("background-image","url('"+setting.img+"')");
				current.css("background-repeat","no-repeat");
				current.css("background-position","center center");
			}else if(setting.type == "center-repeat"){
				current.css("background-image","url('"+setting.img+"')");
				current.css("background-position","center center");
			}else if(setting.type == "stretch"){
				var img = $("<img class='jdmer_background_img' src='"+setting.img+"'/>");
				current.append(img);
			}
		}
		return current;
	};
	
	
	
	
	/**
	 * Ajax请求器
	 * @author 赵振华
	 */
	$.fn.loader = function(_config){
		var _this = this;
		var config = {
			url : null,
			data : {},
			callback : null,
			animation : true
		};
		$.extend(config,_config);
		if(config.animation){
			_.AnimateLayer.open();
			//_.Mger.alert("开始请求 [ " + config.url + " ]");
		}
		
		$.get(config.url,config.data,function(responseText, status, jXHR){
			$(_this).html(responseText);
			if(config.animation){
				_.AnimateLayer.close();
				//_.Mger.success("完成对 [ " + config.url + " ] 的请求。");
			}
			if(typeof config.callback == "function"){
				config.callback(responseText, status, jXHR);
			}
		});
		return this;
	};
	



	/**
	 * 布局框架 - border
	 * @author 赵振华
	 * 这种布局方式吧整个页面分为五个区域。
	 * 上下左右中五个区域。
	 * 上下俩个区域可以设置高度，但是不能这是宽度。左右区域不能设置高度，只能设置宽度
	 */
	$.fn.borderLayout = function(_config){
		var current = $(this);
		current.addClass("jdmer_border_layout");
		
		var config = {
			selector : ".border_layout"
		};
		$.extend(config,_config);
		
		current.css("overflow","hidden");
		//夫级的信息（宽和高）
		var parent = current.parent();
		parent.css("overflow","hidden");
		
		var parentHeight = parent.height();
		var parentWidth = parent.width();
		
		//获取盒子模型的框
		var getBoxBorder = function(_obj){
			var obj = _obj;
			var boxBorder = {
				x : 0,
				y : 0
			};
			
			var box = obj.box();
			var marginX = box.marginX;
			var marginY = box.marginY;
			var paddingX = box.paddingX;
			var paddingY = box.paddingY;
			var borderX = box.borderX;
			var borderY = box.borderY;
			
			boxBorder.x = marginX + paddingX + borderX;
			boxBorder.y = marginY + paddingY + borderY;
			
			return boxBorder;
		};
		
		/**
		 * 处理百分比
		 */
		var percentHandle = function(_str,_a){
			var value = 0;
			if(_a == "width"){
				value = current.width();
			}else if(_a == "height"){
				value = current.height();
			}
			_str = _str.replace("%", "");
			var num = Number(_str);
			return Math.ceil( value * (num / 100) );
		};
		
		var currentBoxBorder = getBoxBorder(current);
		
		//设置当前对象的宽度和高度
		current.width(parentWidth - currentBoxBorder.x);
		current.height(parentHeight - currentBoxBorder.y);
		
		var currentWidth = current.width();
		var currentHeight = current.height();
		
		//获取当前对象的子对象
		var north = current.findUncross("[region='north']","[changer='borderLayout']");
		var west = current.findUncross("[region='west']","[changer='borderLayout']");
		var center = current.findUncross("[region='center']","[changer='borderLayout']");
		var east = current.findUncross("[region='east']","[changer='borderLayout']");
		var south = current.findUncross("[region='south']","[changer='borderLayout']");
		
		//重新整理顺序
		var cP = center.parent();
		north.appendTo(cP);
		west.appendTo(cP);
		center.appendTo(cP);
		east.appendTo(cP);
		south.appendTo(cP);

		var northHeight = 0;
		var southHeight = 0;
		var westWidth = 0;
		var eastWidth = 0;

		var northBoxBorder = getBoxBorder(north);
		north.width(currentWidth - northBoxBorder.x);
		var northOverflow = north.css("overflow");
		var northHeightStr = north.attr("height");
		if (northHeightStr != undefined) {
			if(northHeightStr.indexOf("%") > 0){
				northHeight = percentHandle(northHeightStr,"height") + northBoxBorder.y;
			}else{
				var temp = Number(northHeightStr);
				if (temp) {
					northHeight = temp + northBoxBorder.y;
				}
			}
		}else{
			var domNorth = north.height(1).css("overflow","auto").get(0);
			var nscrollHeight = 0;
			if( domNorth ){
				nscrollHeight = domNorth.scrollHeight;
			}
			northHeight = nscrollHeight;
		}
		north.css("overflow",northOverflow);
		northHeight = northHeight;

		var southBoxBorder = getBoxBorder(south);
		south.width(currentWidth - southBoxBorder.x);
		var southOverflow = north.css("overflow");
		var southHeightStr = south.attr("height");
		if (southHeightStr != undefined) {
			if(southHeightStr.indexOf("%") > 0){
				southHeight = percentHandle(southHeightStr,"height")+ southBoxBorder.y;
			}else{
				var temp = Number(southHeightStr);
				if (temp) {
					southHeight = temp+ southBoxBorder.y;
				}
			}
		}else{
			var domSouth = south.height(1).css("overflow","auto").get(0);
			var sscrollHeight = 0;
			if( domSouth ){
				sscrollHeight = domSouth.scrollHeight;
			}
			southHeight = sscrollHeight;
		}
		south.css("overflow",southOverflow);
		southHeight = southHeight ;

		var westBoxBorder = getBoxBorder(west);
		var westWidthStr = west.attr("width");
		if (westWidthStr != undefined) {
			if(westWidthStr.indexOf("%") > 0){
				westWidth = percentHandle(westWidthStr,"width") + westBoxBorder.x;
			}else{
				var temp = Number(westWidthStr);
				if (temp) {
					westWidth = temp + westBoxBorder.x;
				}
			}
		}
		westWidth = westWidth;

		var eastBoxBorder = getBoxBorder(east);
		var eastWidthStr = east.attr("width");
		if (eastWidthStr != undefined) {
			if(eastWidthStr.indexOf("%") > 0){
				eastWidth = percentHandle(eastWidthStr,"width") + eastBoxBorder.x;
			}else{
				var temp = Number(eastWidthStr);
				if (temp) {
					eastWidth = temp + eastBoxBorder.x;
				}
			}
		}
		eastWidth = eastWidth;
		
		var centerHeight = currentHeight - northHeight - southHeight;
		var centerWidth = currentWidth - westWidth - eastWidth;
		
		north.height(northHeight - northBoxBorder.y);
		north.css("clear","left");
		
		var westBoxBorder = getBoxBorder(west);
		west.height(centerHeight - westBoxBorder.y);
		west.width(westWidth -westBoxBorder.x);
		west.css("float","left");
		
		var centerBoxBorder = getBoxBorder(center);
		center.height(centerHeight - centerBoxBorder.y);
		center.width(centerWidth - centerBoxBorder.x);
		center.css("float","left");
		
		var eastBoxBorder = getBoxBorder(east);
		east.height(centerHeight - eastBoxBorder.y);
		east.width(eastWidth - eastBoxBorder.x);
		east.css("float","left");
		
		south.height(southHeight - southBoxBorder.y);
		south.css("clear","left");
		
		var northChildrenLayout = north.find(config.selector);
		if(northChildrenLayout.size() > 0){
			northChildrenLayout.eq(0).borderLayout(config);
		}
		var westChildrenLayout = west.find(config.selector);
		if(westChildrenLayout.size() > 0){
			westChildrenLayout.eq(0).borderLayout(config);
		}
		var centerChildrenLayout = center.find(config.selector);
		if(centerChildrenLayout.size() > 0){
			centerChildrenLayout.eq(0).borderLayout(config);
		}
		var eastChildrenLayout = east.find(config.selector);
		if(eastChildrenLayout.size() > 0){
			eastChildrenLayout.eq(0).borderLayout(config);
		}
		var southChildrenLayout = south.find(config.selector);
		if(southChildrenLayout.size() > 0){
			southChildrenLayout.eq(0).borderLayout(config);
		}
		
		current.resizCall();	//触发重置大小事件
		
		return current;
	};
	
	if(!_.Changer){
		return ;
	}
	_.Changer.addHandler({
		name : "layout",
		selector : "div[changer='borderLayout']",
		handler : function(objs , parent){
			objs.eq(0).borderLayout({
				selector : "div[changer='borderLayout']"
			}).attr("changer","borderLayout");
		}
	});
	

	
	
	
	
	
	
	/**
	 * jQuery插件集
	 * @author 赵振华
	 * jDreamer提供的jQuey插件，这些功能可以实现一些较为负责的功能
	 */
	/**
	 * 继承jQuery.fn
	 * */
	$.extend($.fn,{
		/**
		 * 更具一个class名称，获取这个class引用的背景图片。
		 * @param _className	css样式名
		 * @returns
		 */
		getImageFromClass : function(_className){
			
			var current = $(this);
			current.addClass(_className);
			//获取图片
			var img = current.css("background-image");
			if(img){//处理图片
				img = img.substring(5, img.length);
				img = img.substring(0 , img.length - 2);
				if( img == "" ){
					img = undefined;
				}else{
					//兼容苹果和google浏览器
					if(img.indexOf("http") < 0){
						img = current.css("background-image");
						//处理图片
						img = img.substring(4, img.length);
						img = img.substring(0 , img.length - 1);
					}
				}
			}
			current.removeClass(_className);
			return img;
		},
		/**
		 * 获取当前对象上面注册的jQuery事件
		 * @param _name	事件名
		 * @returns
		 */
		getEvent : function(_name){
			var allevent = [];
			try{
				var elemData = $._data($(this)[0]);
				var events = elemData.events;
				var handlers = events[ _name ];
				for(i in handlers){
					allevent[i] = handlers[i].handler;
				}
			}catch(e){alert(e);}
			return allevent;
		},
		/**
		 * 鼠标经过和移出的效果
		 * @param _class	样式名
		 * @returns
		 */
		mouseoverout : function(_class){	
			var $this = $(this);
			$this.mouseover(function() {
				$(this).addClass(_class);
			}).mouseout(function() {
				$(this).removeClass(_class);
			});
			return $this;
		},
		/**
		 * 鼠标按下和放开的效果
		 * @param _class	样式名
		 * @returns
		 */
		mousedownup : function(_class){
			var $this = $(this);
			$this.mousedown(function() {
				$(this).addClass(_class);
			}).mouseup(function() {
				$(this).removeClass(_class);
			});
			return $this;
		},
		/**
		 * 获取当前对象上面的背景图片
		 * @returns
		 */
		getBackgroundImage : function(){
			var current = $(this);
			//获取图片
			var img = current.css("background-image");
			if(img != "none"){
				//处理图片
				img = img.substring(5, img.length);
				img = img.substring(0 , img.length - 2);
				if( img == "" ){
					img = undefined;
				}else{
					
					//兼容苹果和google浏览器
					if(img.indexOf("http") < 0){
						img = current.css("background-image");
						//处理图片
						img = img.substring(4, img.length);
						img = img.substring(0 , img.length - 1);
					}
				}
			}else{
				img = undefined;
			}
			return img;
		},
		/**
		 * 大小发生改变时触发
		 * @param _fn	事件
		 * @param _fnName	事件名称
		 */
		resizCall : function(_fn,_fnName){
			var current = $(this);
			if(_fn != undefined){
				var map = current.data("resizCallMap");
				if(map == undefined){
					map = new _.Map();
				}
				if(_fnName == undefined){
					_fnName = _.Util.guid();
				}
				map.put(_fnName,_fn);
				current.data("resizCallMap",map);
			}else{	//回调
				
				var resizCallTimeId = current.data("resizCallTimeId");
				clearTimeout(resizCallTimeId);
				resizCallTimeId = setTimeout(function(){
					var map = current.data("resizCallMap");
					if(map == undefined){
						map = new _.Map();
					}
					var oldHeight = current.data("oldHeight");
					var oldWidth = current.data("oldWidth");
					var height = current.height();
					var width = current.width();
					if(oldWidth != width || oldHeight != height){
						map.call(function(_val,_key){
							_val(current);
						});
						current.data("oldHeight",height);
						current.data("oldWidth",width);
					}
					var childrens = current.children();
					for(var i = 0;i<childrens.size();i++){
						var children = childrens.eq(i);
						children.resizCall();
					}
				},10);
				current.data("resizCallTimeId",resizCallTimeId);
			}
			return current;
		},
		/**
		 * 移除大小改变触发的事件
		 * @param _fnName	事件名
		 */
		removeResizCall:function(_fnName){
			var current = $(this);
			var map = current.data("resizCallMap");
			if(map){
				map.remove(_fnName);
			}
			current.data("resizCallMap",map);
			return current;
		},
		/**
		 * 获取盒子模型的值
		 * @returns {___anonymous3555_4003}
		 */
		box : function() {
			var current = $(this);

			var box = {
				borderTop : 0,
				borderBottom : 0,
				borderLeft : 0,
				borderRight : 0,

				marginTop : 0,
				marginBottom : 0,
				marginLeft : 0,
				marginRight : 0,

				paddingTop : 0,
				paddingBottom : 0,
				paddingLeft : 0,
				paddingRight : 0,

				paddingX : 0,
				paddingY : 0,

				borderX : 0,
				borderY : 0,

				marginX : 0,
				marginY : 0,

				height : 0,
				width : 0,

				x : 0,
				y : 0
			};

			var getWidth = function(_target) {
				var str = current.css(_target);
				if(str == undefined){
					str = "0px";
				}
				return Number(str.substring(0, str.indexOf("px")));
			};

			box.marginTop = getWidth("margin-top");
			box.marginLeft = getWidth("margin-left");
			box.marginRight = getWidth("margin-right");
			if ($.browser.safari)
				box.marginRight = getWidth("margin-left"); // safari浏览器margin-right有bug
			box.marginBottom = getWidth("margin-bottom");

			box.paddingTop = getWidth("padding-top");
			box.paddingLeft = getWidth("padding-left");
			box.paddingRight = getWidth("padding-right");
			box.paddingBottom = getWidth("padding-bottom");

			box.borderTop = getWidth("border-top-width");
			box.borderLeft = getWidth("border-left-width");
			box.borderRight = getWidth("border-right-width");
			box.borderBottom = getWidth("border-bottom-width");

			box.marginX = box.marginLeft + box.marginRight;
			box.marginY = box.marginTop + box.marginBottom;

			box.borderX = box.borderLeft + box.borderRight;
			box.borderY = box.borderBottom + box.borderTop;

			box.paddingX = box.paddingLeft + box.paddingRight;
			box.paddingY = box.paddingBottom + box.paddingTop;

			box.height = box.marginY + box.paddingY + box.borderY
					+ current.height();
			box.width = box.marginX + box.paddingX + box.borderX
					+ current.width();

			box.y = box.marginY + box.paddingY + box.borderY;
			box.x = box.marginX + box.paddingX + box.borderX;

			return box;
		},
		/**
		 * 上级元素的文档偏移值
		 * @returns {___anonymous6618_6693}
		 */
		parentOffset : function(){
			var parent = this.parent();
			//获取元素的纵坐标
			function getTop(e){
				var offset=e.offsetTop;
				if(e.offsetParent!=null)
					offset+=getTop(e.offsetParent);
				return offset;
			}
			//获取元素的横坐标
			function getLeft(e){
				var offset=e.offsetLeft;
				if(e.offsetParent!=null)
					offset+=getLeft(e.offsetParent);
				return offset;
			}
			
			var offset = {
				top : getTop(parent.get(0)),
				left : getLeft(parent.get(0))
			};
			return offset;
		},
		/**
		 * 接受一个选择器，遇到选择器。停止查找
		 * @param _selector
		 * @param _by
		 * @returns
		 */
		findUncross : function(_selector,_by){
			var current = $(this);
			var tempSelector = ".jdmer_no_children_can_find";
			
			var findChildren = function(obj){
				var child = obj.children();
				if(child.size() > 0){
					for(var i = 0; i < child.size();i++){
						var cur = child.eq(i);
						if(cur.is(_by)){	//返回空
							children =  current.children(tempSelector);
							break;
						}else if(cur.is(_selector)){
							children = cur;
							break;
						}else{
							findChildren(cur);
						}
					};
				}else{	//如果没有子元素，返回空
					children =  current.children(tempSelector);
				}
			};
			findChildren(current);
			
			return children;
		},
		/**
		 * 依据百分比，获取这个对象大小
		 * @param percent	百分比
		 */
		sizeValueByPercent : function(percent){
			var size = {
				height : 0,
				width : 0
			};
			var current = this;
			percent = percent.replace("%", "");
			percent = Number(percent);
			size.height = Math.ceil(current.height() * percent / 100);
			size.width = Math.ceil(current.width() * percent / 100);
			return size;
		},
		/**
		 * 通过表达式查找父级元素，第一次查到后停止查询并返回。
		 * 例如 ：$("#demo").parentSole([".test2",".test1"]);
		 * 会返回.test2 或 .test1，但只会返回一个距离当前元素最近的。
		 * 
		 * @param exp	数组或者字符串
		 * @returns
		 */
		parentFirstFind : function(exp){
			var current = $(this);
			var container = undefined;
			var find = function(_obj){
				if(_obj.get(0) != undefined){
					var parent = _obj.parent();
					
					if(typeof exp == "string"){
						if(parent.is(exp)){
							container = parent;
						}else if(parent.get(0) != undefined){
							find(parent);
						}
					}else if(typeof exp == "object"){
						var is = "";
						for(i in exp){
							if(i == exp.length - 1)
								is = is + "parent.is('"+exp[i]+"')";
							else
								is = is + "parent.is('"+exp[i]+"') || ";
						}
						if(eval(is)){
							container = parent;
						}else if(parent.get(0) != undefined){
							find(parent);
						}
					}
					
					
				}
			};
			find(current);
			return container;
		}
	});
	
})(jDreamer , jQuery);